<?php
/**
 * CK框架 表单类
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
final class form
{
	/**
	 * 从数组中获得表单
	 * @param array $formArray
	 * 表单数组信息
	 * @param array $values
	 * 表单值
	 * @return 
	 */
	public static function formFromArray($formArray,$values = array())
	{
		if(is_array($formArray)) foreach ($formArray as $fKey => $fValue)
		{
			isset($values[$fKey]) && !empty($values[$fKey]) ? $fValue['value'] = $values[$fKey] : '';
			echo self::getTrString($fValue);
		}
	}
	/**
	 * 从配置文件中载入表单
	 * @param string $fileName
	 * 表单的配置文件
	 * @param Array $values
	 * 表单对应的值
	 * @return
	 */
	public static function formFromFile($fileName,$values = array())
	{
		$formFile = CK_APPLICATION_DATA_DIR.'forms/'.$fileName.'.php';
		if(!file_exists($formFile))
		{
			echo $fileName.' form can\'t found';
		} else 
		{
			$formArr = include($formFile);
			self::formFromArray($formArr,$values);
		}
	}
	const verifyKey = 'aydefdsw';
	/**
	 * 获取表单验证
	 * @param string $formName 表单名
	 * @return string
	 */
	public static function getVerifyKey($formName)
	{
		return '<input type="hidden" value="'.self::getVerifyKeyString($formName).'" name="verifyKey" id="verifyKey">';
	}
	/**
	 * 获取表单验证字符串
	 * @param string $formName
	 * @return string
	 */
	public static function getVerifyKeyString($formName)
	{
		cookie::set($formName, SYSTIME);
		return md5(form::verifyKey . SYSTIME . $formName);
	}
	/**
	 * 校验验证码
	 * @param string $formName
	 * @return bool
	 */
	public static function checkVerifyKey($formName)
	{
		$cookieValue = cookie::get($formName);
		if(!$cookieValue)
		{
			return false;
		}
		return isset($_REQUEST['verifyKey']) && $_REQUEST['verifyKey'] && $_REQUEST['verifyKey'] == md5(form::verifyKey . $cookieValue . $formName);
	}
	/**
	 * 获取表单的TR表格信息
	 * @param array $inputArr
	 * 表单数组
	 * @return string
	 */
	public static function getTrString ($inputArr)
	{
		if (! $inputArr)
		{
			return '';
		}
		$string = '<tr><th width="150">' . $inputArr['tipsName'] . '：</th><td>';
		$inputArr['name2'] = $inputArr['name'];
		$inputArr['name'] = 'poster[' . $inputArr['name'] . ']';
		switch ($inputArr['type'])
		{
			case 'input':
				$string .= self::input($inputArr['name'], $inputArr['id'], $inputArr['value'], $inputArr['jsEvent'], $inputArr['className']);
				break;
			case 'password':
				$string .= self::password($inputArr['name'],$inputArr['id'], $inputArr['jsEvent'], $inputArr['className']);
				break;
			case 'select':
				$string .= self::select($inputArr['name'], $inputArr['id'], 
				$inputArr['value'], $inputArr['jsEvent'], $inputArr['className'], 
				$inputArr['opArray']);
				break;
			case 'calendar':
				$string .= self::calendar($inputArr['name'], $inputArr['id'], 
				$inputArr['value'], $inputArr['jsEvent'], $inputArr['className'], 
				$inputArr['dateFormat']);
				break;
			case 'textarea':
				! $inputArr['rows'] ? $inputArr['rows'] = 6 : '';
				! $inputArr['cols'] ? $inputArr['cols'] = 50 : '';
				$string .= self::textArea($inputArr['name'], $inputArr['id'], 
				$inputArr['value'], $inputArr['jsEvent'], $inputArr['className'], 
				$inputArr['rows'], $inputArr['cols']);
				break;
			case 'radio':
				$string .= self::radio($inputArr['name'], $inputArr['value'], 
				$inputArr['opArray'], $inputArr['className'], 
				$inputArr['jsEvent']);
				break;
			case 'checkbox':
				$string .= self::checkbox($inputArr['name'], $inputArr['value'], 
				$inputArr['opArray'], $inputArr['className'], 
				$inputArr['jsEvent']);
				break;
			case 'file':
				$string .= self::file($inputArr['name2'], $inputArr['id'], $inputArr['className'], $inputArr['jsEvent']);
				break;
			case 'editor':
				if (! $inputArr['id'])
				{
					$inputArr['id'] = $inputArr['name'];
				}
				$string .= self::editor($inputArr['name'], $inputArr['id'], $inputArr['value']);
				break;
		}
		$string .= '<font color="gray">'.$inputArr['description'].'</font></td></tr>';
		return $string;
	}

	/**
	 * 构建input表单
	 * 
	 */
	public static function input ($name, $id = '', $value = '', $jsEvent = '', $className = 'input-text')
	{
		return '<input name="' . $name . '" id="' . $id . '" ' . $jsEvent .' value="' . $value . '" class="' . $className .'" type="text" size="25">';
	}
	/**
	 * password表单
	 * @param string $name
	 * 控件名字
	 * @param string $id
	 * 控件ID
	 * @param string $jsEvent
	 * 控件JAVASCRIPT事件
	 * @param string $className
	 * 控件className
	 */
	public static function password($name,$id='',$jsEvent='',$className = 'input-text')
	{
		return '<input name="' . $name . '" id="' . $id . '" ' . $jsEvent .' class="' . $className .'" type="password" size="25">';
	}
	/**
	 * 构建select选项
	 * @param string $name 控件name属性
	 * @param string $id 控件ID属性
	 * @param string $value 值
	 * @param string $jsEvent 控件js事件
	 * @param string $className 控件class类名
	 * @param array $options 数组value对应值 key对应显示
	 * @return string
	 */
	public static function select ($name, $id = '', $value = '', $jsEvent = '', $className = '', $options = array())
	{
		$string = '<select name="' . $name . '" class="' . $className .
		 '" id="' . $id . '" ' . $jsEvent . '>';
		$select = '';
		if (is_array($options) && $options)
		{
			foreach ($options as $opKey => $opValue)
			{
				$select = '';
				if ($opKey == $value)
				{
					$select = ' selected="true" ';
				}
				$string .= '<option value="' . $opKey . '" ' . $select . '>' .
				 $opValue . '</option>';
			}
		}
		$string .= '</select>';
		return $string;
	}

	/**
	 * 日历选择
	 * @param string $name 控件名
	 * @param string $id 控件ID
	 * @param string $value 控件值
	 * @param string $jsEvent 控件js事件
	 * @param string $className 控件Classname
	 */
	public static function calendar ($name, $id = '', $value = '', $jsEvent = '', $className = 'date', $dateFormat = '%Y-%m-%d %H:%M:%S')
	{
		$string = '';
		! $id ? $id = $name : '';
		! $dateFormat ? $dateFormat = '%Y-%m-%d %H:%M:%S' : '';
		! $className ? $className = 'date' : '';
		if (! self::$_hasCalendar)
		{
			$string .= '<link rel="stylesheet" type="text/css" href="/static/js/calendar/jscal2.css"/>
			<link rel="stylesheet" type="text/css" href="/static/js/calendar/border-radius.css"/>
			<link rel="stylesheet" type="text/css" href="/static/js/calendar/win2k.css"/>
			<script type="text/javascript" src="/static/js/calendar/calendar.js"></script>
			<script type="text/javascript" src="/static/js/calendar/lang/en.js"></script>';
			self::$_hasCalendar = true;
		}
		$jsEvent .= ' readonly size="21" ';
		$string .= self::input($name, $id, $value, $jsEvent, $className);
		$string .= '<script type="text/javascript">
			Calendar.setup({
			weekNumbers: true,
		    inputField : "' . $id . '",
		    trigger    : "' . $id . '",
		    dateFormat: "' . $dateFormat . '",
		    showTime: true,
		    minuteStep: 1,
		    onSelect   : function() {this.hide();}
			});
        </script>';
		return $string;
	}

	public static function textArea ($name, $id = '', $value = '', $jsEvent = '', 
	$className = 'input-text', $rows = 6, $cols = 50)
	{
		return '<textarea id="' . $id . '" rows="' . $rows . '" cols="' . $cols .
		 '" class="' . $className . '" ' . $jsEvent . ' name="' . $name .'">' . $value . '</textarea>';
	}

	public static function radio ($name, $value = '', $options = array(), $className = '', 
	$jsEvent = '')
	{
		if (! $options)
		{
			return '';
		}
		$str = $checked = '';
		foreach ($options as $optionKey => $optionValue)
		{
			$checked = $optionKey == $value ? 'checked="true"' : '';
			$str .= '<input type="radio" name="' . $name . '" class="' .
			 $className . '" value="' . $optionKey . '" ' . $jsEvent . ' ' .
			 $checked . '>' . $optionValue . ' &nbsp;&nbsp;';
		}
		return $str;
	}

	public static function checkbox ($name, $values = array(), $options = array(), 
	$className = '', $jsEvent = '')
	{
		if (! $options)
		{
			return '';
		}
		$str = $checked = '';
		$hasValue = is_array($values) && $values;
		foreach ($options as $optionKey => $optionValue)
		{
			$checked = $hasValue && in_array($optionKey, $values) ? 'checked="true"' : '';
			$str .= '<input type="checkbox" name="' . $name . '" class="' .
			 $className . '" value="' . $optionKey . '" ' . $jsEvent . ' ' .
			 $checked . '>' . $optionValue . '&nbsp;&nbsp;';
		}
		return $str;
	}

	public static function file ($name, $id = '', $className = '', $jsEvent = '')
	{
		return '<input type="file" id="' . $id . '" name="' . $name . '" class="' .$className . '" ' . $jsEvent . '></input>';
	}

	/**
	 * 编辑器
	 * 本函数使用的是baidu编辑器。需要在网站根目录添加ueditor百度编辑器目录
	 */
	public static function editor ($name, $id, $value = '')
	{
		$string = '<script type="text/javascript" charset="utf-8" src="/ueditor/editor_config.js"></script>
    				<script type="text/javascript" charset="utf-8" src="/ueditor/editor_all_min.js"></script>
		<link rel="stylesheet" type="text/css" href="/ueditor/themes/default/ueditor.css"/>';
		$string .= '<script type="text/plain" id="' . $id . '">' .$value . '</script>';
		$string .= '<script>
		var editor = new baidu.editor.ui.Editor({textarea:"'.$name.'"});
		editor.render("'.$id.'");
		</script>';
		return $string;
	}

	private static $_hasCalendar = false;

}